#ifndef GAME_H
#define GAME_H

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <vector> 
#include <deque>
#include "./random.h"

using std::vector;
using std::deque;

class Game {
private:
struct Point {
    Point() {};
    Point(int _x, int _y) : x(_x), y(_y) {};
    
    int x, y;

    Point operator+(const Point &b) {
        return Point(x + b.x, y + b.y);
    }
    void operator+=(const Point &b) {
        x += b.x; y += b.y;
    }
    Point operator-(const Point &b) {
        return Point(x - b.x, y - b.y);
    }
    Point operator-() {
        return Point(-x, -y);
    }
    void operator-=(const Point &b) {
        x -= b.x; y -= b.y;
    }
    bool operator==(const Point &b) {
        return x == b.x && y == b.y;
    }
    bool operator!=(const Point &b) {
        return x != b.x || y != b.y;
    }
};

struct Snake {
    deque<Point> points;
    Point direction;
};

struct Record {
	Point init_tail, init_direction;
	vector<int> operation;
	vector<Point> food;
	
    int operation_i, food_i;
};

public:
int score, time, status;

private:
int height, width, initlen;
Snake snake;
vector< vector<bool> > map;
Point food;
Record record;

Point numToDirection(int num);
void newFood();
bool checkOver(Point p);

public:
Game() : score(0), time(0), status(0) {}
Game(int height, int width, int initlen);
Game(const char *filename);

void display();
// 导出目前游戏局势，以传给ANN
void outputData(bool arr[]);

/* 游戏主函数
返回值含义
0：正常
-1：蛇死亡
1：该步操作无效
*/
int go(int direction_num);

void initReplay();

/* 游戏记录回放 
返回值含义
0：正常
-1：蛇死亡
-2：游戏被手动结束
1：改步操作无效
*/
int next();

void saveRecord(const char *filename);

};

#include "game.cpp"

#endif